import json
from ronglian_sms_sdk import SmsSDK

accId = '2c94811c9035ff9f019279b664be7118'
accToken = 'da7abd16b090476ab08895ffbda56211'
appId = '2c94811c9035ff9f019279bc2eea7124'
        
# def send_message():
#     #初始化
#     sdk = SmsSDK(accId, accToken, appId)
#     tid = '1'
#     mobile = '17332694252'
#     datas = ('1234','5')
#     resp = sdk.sendMessage(tid, mobile, datas)
#     print(resp)
    
class CCP(object):
    # "实现单例类"
    def __new__(cls,*args,**kwargs):
        #判断单例是否存在,不存在就初始化单例
        if not hasattr(cls,'_instance'):
            #super，找到父对象，父对象调用__new__创建对象，动态赋值_instance
            cls._instance = super(CCP,cls).__new__(cls,*args,**kwargs)
            #初始化
            cls._instance.sdk = SmsSDK(accId, accToken, appId)
        #存在立即返回
        return cls._instance
    #发送短信验证码
    def send_message(self,tid, mobile, datas):
        resp = self.sdk.sendMessage(tid, mobile, datas)
        dict = json.loads(resp)
        if dict.get('statusCode') == '000000':
            return 0
        else:
            return -1
    
# if __name__ == '__main__':
#     tid = '1'
#     mobile = '17332694252'
#     datas = ('6666','5')
#     #调用单例类发送短信验证码
#     if CCP().send_message(tid,mobile,datas) == 0:
#         print("发送成功")
#     else:
#         print("发送失败")    